﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace FoundationLib.Common.Collections
{
	public class GroupedListItem<TKey, TElement> : IGrouping<TKey, TElement>
	{
		private TKey key;
		private List<TElement> elements;


		public GroupedListItem()
		{
			this.elements = new List<TElement>();
		}

		public GroupedListItem(TKey key)
			: this()
		{
			Contract.Requires(key != null);
			Contract.Ensures(this.key.Equals(key));

			this.key = key;
		}

		public GroupedListItem(TKey key, IEnumerable<TElement> elements)
			: this(key)
		{
			Contract.Requires(elements != null);
			this.elements.AddRange(elements);
		}

		public void Add(TElement element)
		{
			this.elements.Add(element);
		}

		public bool Remove(TElement element)
		{
			return this.elements.Remove(element);
		}

		#region IGrouping<TKey,TElement> Members

		public TKey Key
		{
			get { return this.key; }
		}

		#endregion

		#region IEnumerable<TElement> Members

		public IEnumerator<TElement> GetEnumerator()
		{
			return this.elements.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return ((System.Collections.IEnumerable)this.elements).GetEnumerator();
		}

		#endregion
	}
}
